package xquery;

import java.util.ArrayList;

import org.w3c.dom.Node;

import xquery.XPathComposedExp.XPathOperator;

public class XQueryPathExp extends XQuery {
	XQExpression xquery;
	XPathComposedExp.XPathOperator relation;
	XQExpression xpath;
	
	@Override
	public XPathData eval(XQContext context, ArrayList<Node> currentNodeList) {
		XPathData result = xquery.eval(context, currentNodeList);
		
		if(relation == XPathOperator.DIRECT) {
			return xpath.eval(context, result.getList());
		} else if (relation == XPathOperator.ALL) {
			ArrayList<Node> resultNodeList = getDescendants(context, result.getList());
			return xpath.eval(context, resultNodeList);
		}	
		return null;
	}
	public String toString()
	{
		StringBuffer sb = new StringBuffer("");
		sb.append(xquery);
		if(relation == XPathOperator.DIRECT) {
			sb.append("\\");
		}
		else
		{
			sb.append("\\\\");
		}
		sb.append(xpath);
		return sb.toString();
	
	}
}
